package evs.idl;

import java.io.File;
import java.io.IOException;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 * A
 */
public class SimpleIDLCompiler {
 
    /**
     * Accept one command line argument: the name of an XML IDL Description File
     */
    public static void main(String[] args) {
		try {
	        if (args.length < 1) {
	            System.err.println("Usage:");
	            System.err.println("  java " + SimpleIDLCompiler.class.getName(  )
	                    + " xmlFileName [resourcesDir] [outputDir]");
	            System.exit(1);
	        }
	
	        File xmlFile = new File(args[0]);
			String basedir = (args[1].length() == 0) ? "resources" : args[1];
			String outDir = (args[2].length() == 0) ? "src" : args[2];
			

	    trans(xmlFile, new File(basedir + "/idl2all.xsl"), outDir);
        
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
    }

    private static void trans(File xmlFile, File xslFile, String outDir) throws javax.xml.transform.TransformerException{
        Source xmlSource = new StreamSource(xmlFile);
        Source xsltSource = new StreamSource(xslFile);
        StreamResult result =  new StreamResult(System.out);
        System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
        TransformerFactory transFact = javax.xml.transform.TransformerFactory.newInstance(  );
        
        Transformer trans = transFact.newTransformer(xsltSource);
        trans.setParameter("outPath", outDir);
        trans.transform(xmlSource, result);
        System.out.print(result);
    }

}

